package com.example.springboot.service;

import com.example.springboot.entity.TruckOrder;
import com.example.springboot.entity.TruckOrderExample;
import com.example.springboot.mapper.TruckOrderMapper;
import com.example.springboot.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: jyf2000jyf
 * @Date: 2022/5/12
 * @Time: 9:00
 * @Email: 904593011@qq.com
 * @Description:
 **/
@Service
public class AdminOrderServiceImpl implements AdminOrderService {
    @Autowired
    private TruckOrderMapper orderMapper;

    @Override
    public Integer save(TruckOrder order) {
        Date date = new Date();
        order.setUpdateTime(date);
        if (order.getStatus() == 4){
            order.setFinishTime(date);
        }
        return orderMapper.updateByPrimaryKeySelective(order);

    }

    @Override
    public List<TruckOrder> findAll() {
        return orderMapper.selectByExample(null);
    }

    @Override
    public Integer delete(Integer id) {
        return orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<TruckOrder> findAll(Integer pageNum, Integer pageSize, String company, Integer status) {
        TruckOrderExample example = new TruckOrderExample();
        TruckOrderExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause("id desc limit " + pageNum + "," + pageSize);
        if (company != null && !"".equals(company)) {
            criteria.andCompanyLike("%" + company + "%");
        }
        if (status != null && !status.equals(-1)) {
            criteria.andStatusEqualTo(status.byteValue());
        }
        return orderMapper.selectByExample(example);
    }

    @Override
    public Integer getAmount(String company, Integer status) {
        TruckOrderExample example = new TruckOrderExample();
        TruckOrderExample.Criteria criteria = example.createCriteria();
        if (company != null && !"".equals(company)) {
            criteria.andCompanyLike("%" + company + "%");
        }
        if (status != null && !status.equals(-1)) {
            criteria.andStatusEqualTo(status.byteValue());
        }
        return (Integer) (int) orderMapper.countByExample(example);
    }

    @Override
    public ArrayList<Integer> initFlow(String date) {
        String start;
        String end;
        ArrayList<Integer> list = new ArrayList<>();
        List<TruckOrder> truckOrders;
        for (int i = 0; i < 24; i++) {
            if (i < 9) {
                start = date + " 0" + i + ":00:00";
                end = date + " 0" + (i + 1) + ":00:00";
            }else if(i == 9){
                start = date + " 0" + i + ":00:00";
                end = date + " " + (i + 1) + ":00:00";
            }else{
                start = date + " " + i + ":00:00";
                end = date + " " + (i + 1) + ":00:00";
            }
            TruckOrderExample example = new TruckOrderExample();
            example.createCriteria().andArrangeTimeBetween(DateUtils.convert(start), DateUtils.convert(end));
            truckOrders = orderMapper.selectByExample(example);
            long amount = 0;
            if (truckOrders != null && truckOrders.size() != 0) {
                amount = orderMapper.sumTAmountByExample(example);
            }
            list.add((int) amount);
        }
        return list;
    }

    @Override
    public Integer getOrderAmount() {
        return orderMapper.countCurrentMonth();
    }

    @Override
    public Integer getFinishedOrderAmount() {
        TruckOrderExample example = new TruckOrderExample();
        example.createCriteria().andStatusEqualTo((byte) 4);
        return (Integer)(int)orderMapper.countByExample(example);
    }
}
